<?php
/**
 * XPHP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * @category	XPHP
 * @package		XPHP_Action
 * @subpackage  XPHP_Action_Attribute
 * @author		XWEB Dev Team
 * @copyright	Copyright (c) 2010-2011 XWEB. (http://xweb.vn)
 * @license		http://xphp.xweb.vn/license.html     GNU GPL License
 * @version		$Id: Model.php 20109 2011-22-08 02:05:09 Mr.UBKey $
 */
/**
 * XPHP_Action_Attribute_Abstract
 * @see XPHP_Action_Attribute_Abstract
 */
require_once 'XPHP/Action/Attribute/Abstract.php';
/**
 * Lớp Action Attribute Model dùng để gán Model vào Action
 * @package		XPHP_Action
 * @subpackage  XPHP_Action_Attribute
 * @author		XWEB Dev Team
 */
class XPHP_Action_Attribute_Model extends XPHP_Action_Attribute_Abstract
{
    /**
     * Model Class
     * @var string
     */
    private $_modelClass;
    /**
     * Gán tham số truyền vào cho Attribute
     */
    public function init ($properties)
    {
        if (isset($properties[0]) && is_string($properties[0])) {
            if (class_exists($properties[0])) {
                $param = $properties[0];
                $this->_modelClass = $param;
            } else
                throw new XPHP_Attribute_Exception(
                'XPHP_Action_Attribute_Model không tìm thấy lớp ' .
                 $properties[0]);
        } else {
            throw new XPHP_Attribute_Exception(
            'XPHP_Action_Attribute_Model bắt buộc tham số truyền vào kiểu string là tên Model');
        }
    }
    /**
     * Sự kiện này được thực thi khi trước khi action được thực thi
     */
    public function onActionExecute ()
    {
        $modelClass = $this->_modelClass;
        $model = new $modelClass();
        $this->_controller->setModel($model);
        $this->_controller->parseModelParams();
    }
}